package com.example.tony.zyfypt_101yl;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.Toast;

import com.example.tony.zyfypt_101yl.bean.LoginBean;
import com.example.tony.zyfypt_101yl.iface.LoginListener;
import com.example.tony.zyfypt_101yl.model.LoginModel;

public class LoginActivity extends AppCompatActivity {
    private Button loginButton, registerButton;
    private EditText idET, pwdEt;
    private Switch sw;
    private String username = "", password = "", sessionid = "";//用户名，密码
    private SharedPreferences sp;
    private LoginListener loginListener = new LoginListener() {
        @Override
        public void onResponse(LoginBean loginBean) {

            System.out.println("result:" + loginBean.getId());
            if (loginBean.getSessionid() != null) {

                sessionid = loginBean.getSessionid().toString();
                saveSP();
                successToNext();

            }
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(getApplicationContext(),"对不起登陆失败",Toast.LENGTH_SHORT).show();
        }
    };


    private void successToNext(){
        Intent intent2 = new Intent();
        intent2.setClass(this, MainActivity.class);//forward为当前activity，forwardTarget为新的activity
        startActivity(intent2);
        finish();
    }



    private void init() {
        idET = (EditText)findViewById(R.id.et_log_username);
        pwdEt = (EditText)findViewById(R.id.et_log_pw);
        sw = (Switch) findViewById(R.id.switch1);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        init();
        sp = getSharedPreferences("login", MODE_PRIVATE);
        //读取sp
        readSP();
    }
    public void login(View view){
        username = idET.getText().toString();
        password = pwdEt.getText().toString();
        if (username.equals(username) && password.equals(password)) {
            saveSP();
            LoginModel loginModel = new LoginModel();
            loginModel.getResult(idET.getText().toString(), pwdEt.getText().toString(), loginListener);
        }

    }
    public void register(View view){
        Intent intent=new Intent(LoginActivity.this,RegisterActivity.class);
        startActivity(intent);
        finish();
    }
    //保存sharedpreferences
    private void saveSP() {
        SharedPreferences.Editor editor = sp.edit();
        editor.putString("username", username);
        editor.putString("password", password);
        editor.putString("sessionID", sessionid);
        editor.putBoolean("remember", sw.isChecked());
        System.out.println(username+" "+password+" "+sessionid+" "+sw.isChecked());
        editor.commit();//提交保存
    }
    private void readSP() {
        String name = sp.getString("username", null);
        String pass = sp.getString("password", null);
        String session = sp.getString("sessionID", null);
        Boolean b = sp.getBoolean("remember", false);
        if (b) {
            idET.setText(name);
            pwdEt.setText(pass);
            sw.setChecked(b);
        }
    }
}
